<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="/0505/js/jq.js"></script>
</head>
<body>
    <input type="text" class="name" placeholder="name">
    <input type="text" class="num" placeholder="num">
    <button class="get">获取商品</button>
    <button class="add">添加商品</button>
    <ul class="goods"></ul>
    <script>

        // 获取商品
        $('.get').click(function() {
            $.ajax({
                url: 'http://localhost:3000/goodslist',
                success(res) {
                    console.log(res);
                    let str = '';
                    let arr = JSON.parse(res).list
                    arr.forEach(item => {
                        str += `<li>${item.name}---${item.num}</li>`
                    });
                    $('.goods').html(str)
                }
            })

        })


        // 新增商品--先收集用户输入的内容，然后发post请求传给后台
        $('.add').click(function(){
            let name = $('.name').val()
            let num = $('.num').val()
            let data1 = {"name": name, "num": num};
            $.ajax({
                // 平常看文档，今天看心情！！！
                url: 'http://localhost:3000/add',
                type: 'post',
                data: data1,
                success(res) {
                    console.log(res);
                }
            })
        })
    </script>
</body>
</html>